﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using IOEntity;
using System.Data.SqlClient;
namespace IODataAccess
{
    public class clsDAInfomationContent
    {
        clsDAConnection con = new clsDAConnection();
        public clsInfomationContent GetById(int id)
        {
            try
            {
                clsInfomationContent obj = new clsInfomationContent();
                string str = "select * from tblInfomationContent where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    obj.Id = id;
                    if (dr["tieude"] != DBNull.Value)
                    {
                        obj.TieuDe = dr["tieude"].ToString();
                    }
                    if (dr["noidung"] != DBNull.Value)
                    {
                        obj.NoiDung = dr["noidung"].ToString();
                    }
                    if (dr["ghichu"] != DBNull.Value)
                    {
                        obj.GhiChu = dr["ghichu"].ToString();
                    }
                    if (dr["hinhanh"] != DBNull.Value)
                    {
                        obj.HinhAnh = dr["hinhanh"].ToString();
                    }
                    if (dr["infoid"] != DBNull.Value)
                    {
                        obj.InfoId = Convert.ToInt32(dr["infoid"].ToString());
                    }
                }
                return obj;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public List<clsInfomationContent> GetAll()
        {
            try
            {
                List<clsInfomationContent> list = new List<clsInfomationContent>();
                string str = "select * from tblinfomationcontent order by id DESC";
                SqlCommand com = new SqlCommand(str, con.getCon());
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    clsInfomationContent obj = new clsInfomationContent();
                    if (dr["tieude"] != DBNull.Value)
                    {
                        obj.TieuDe = dr["tieude"].ToString();
                    }
                    if (dr["noidung"] != DBNull.Value)
                    {
                        obj.NoiDung = dr["noidung"].ToString();
                    }
                    if (dr["ghichu"] != DBNull.Value)
                    {
                        obj.GhiChu = dr["ghichu"].ToString();
                    }
                    if (dr["hinhanh"] != DBNull.Value)
                    {
                        obj.HinhAnh = dr["hinhanh"].ToString();
                    }
                    
                    if (dr["infoid"] != DBNull.Value)
                    {
                        obj.InfoId = Convert.ToInt32(dr["infoid"].ToString());
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public List<clsInfomationContent> GetByLoai(int loaiid)
        {
            try
            {
                List<clsInfomationContent> list = new List<clsInfomationContent>();
                string str = "select * from tblinfomationcontent where infoid=@infoid order by id DESC";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@infoid", loaiid);
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    clsInfomationContent obj = new clsInfomationContent();
                    if (dr["tieude"] != DBNull.Value)
                    {
                        obj.TieuDe = dr["tieude"].ToString();
                    }
                    if (dr["noidung"] != DBNull.Value)
                    {
                        obj.NoiDung = dr["noidung"].ToString();
                    }
                    if (dr["ghichu"] != DBNull.Value)
                    {
                        obj.GhiChu = dr["ghichu"].ToString();
                    }
                    if (dr["hinhanh"] != DBNull.Value)
                    {
                        obj.HinhAnh = dr["hinhanh"].ToString();
                    }
                    if (dr["infoid"] != DBNull.Value)
                    {
                        obj.InfoId = Convert.ToInt32(dr["infoid"].ToString());
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public List<clsInfomationContent> GetByTop(int top)
        {
            try
            {
                List<clsInfomationContent> list = new List<clsInfomationContent>();
                string str = "select * from tblinfomationcontent order by id DESC";
                SqlCommand com = new SqlCommand(str, con.getCon());
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                int dem = 0;
                while (dr.Read() || dem > top)
                {
                    dem++;
                    clsInfomationContent obj = new clsInfomationContent();
                    if (dr["tieude"] != DBNull.Value)
                    {
                        obj.TieuDe = dr["tieude"].ToString();
                    }
                    if (dr["noidung"] != DBNull.Value)
                    {
                        obj.NoiDung = dr["noidung"].ToString();
                    }
                    if (dr["ghichu"] != DBNull.Value)
                    {
                        obj.GhiChu = dr["ghichu"].ToString();
                    }
                    if (dr["hinhanh"] != DBNull.Value)
                    {
                        obj.HinhAnh = dr["hinhanh"].ToString();
                    }
                    if (dr["infoid"] != DBNull.Value)
                    {
                        obj.InfoId = Convert.ToInt32(dr["infoid"].ToString());
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public int Insert(clsInfomationContent obj)
        {
            try
            {
                string str = "insert into tblinfomationcontent(tieude,noidung,ghichu,hinhanh,infoid) values(@tieude,@noidung,@ghichu,@hinhanh,@infoid)";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@tieude", obj.TieuDe);
                com.Parameters.AddWithValue("@noidung", obj.NoiDung);
                com.Parameters.AddWithValue("@ghichu", obj.GhiChu);
                com.Parameters.AddWithValue("@hinhanh", obj.HinhAnh);
                com.Parameters.AddWithValue("@infoid", obj.InfoId);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
                return clsDAStaticMethod.getIDIdentity("tblnews");
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Update(clsInfomationContent obj)
        {
            try
            {
                string str = "Update tblinfomationcontent set tieude=@tieude,noidung=@noidung,ghichu=@ghichu,hinhanh=@hinhanh,infoid=@infoid where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@tieude", obj.TieuDe);
                com.Parameters.AddWithValue("@noidung", obj.NoiDung);
                com.Parameters.AddWithValue("@ghichu", obj.GhiChu);
                com.Parameters.AddWithValue("@hinhanh", obj.HinhAnh);
                com.Parameters.AddWithValue("@infoid", obj.InfoId);
                com.Parameters.AddWithValue("@id", obj.Id);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Delete(int id)
        {
            try
            {
                string str = "delete from tblinfomationcontent where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
    }
}
